package com.example.textsimilarity.controller;

import com.example.textsimilarity.service.TextSimilarityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.POST})
public class TextSimilarityController {
    private static final Logger logger = LoggerFactory.getLogger(TextSimilarityController.class);
    
    private final TextSimilarityService textSimilarityService;

    @Autowired
    public TextSimilarityController(TextSimilarityService textSimilarityService) {
        this.textSimilarityService = textSimilarityService;
    }

    @PostMapping("/compare")
    public ResponseEntity<?> compareTexts(@RequestParam String text1, @RequestParam String text2) {
        logger.info("Received comparison request - Text1: {}, Text2: {}", text1, text2);
        try {
            double similarity = textSimilarityService.calculateSimilarity(text1, text2);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("similarity", similarity);
            logger.info("Comparison successful, similarity: {}", similarity);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("Error processing comparison request", e);
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(error);
        }
    }
} 